//
//  AddContactCell.m
//
//  Created by Olof Millberg on 2010-04-06.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AddContactCell.h"

@implementation AddContactCell
@synthesize addContactButton;
@synthesize delegate;
@synthesize viewController;

#pragma mark Addressbook Related Methods

/* Pushing the people picker */
- (IBAction) addContact:(id)sender
{
	if (!viewController)
		return;
	
	ABPeoplePickerNavigationController *peoplePickerNavigationController
	= [[ABPeoplePickerNavigationController alloc] init];
	
	peoplePickerNavigationController.peoplePickerDelegate = self;
	peoplePickerNavigationController.displayedProperties = [NSArray arrayWithObjects:
															[NSNumber numberWithInt:kABPersonPhoneProperty],
															[NSNumber numberWithInt:kABPersonEmailProperty], nil];
	
	[self.viewController presentModalViewController:peoplePickerNavigationController animated:YES];
	[self navigationController:peoplePickerNavigationController didShowViewController:peoplePickerNavigationController animated:YES];
	[peoplePickerNavigationController release];
}

/* Making the group selection view be the first view */
- (void) navigationController:(UINavigationController *)navigationController 
		didShowViewController:(UIViewController *)viewController 
					 animated:(BOOL)animated 
{ 
	if([navigationController.viewControllers count] > 1) 
	{	navigationController.delegate = nil; 
		[navigationController popViewControllerAnimated:NO];
	} 
}

#pragma mark -

#pragma mark Addressbook Delegate Methods

- (void) peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
	[self.viewController dismissModalViewControllerAnimated:YES];
}

- (BOOL) peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
	   shouldContinueAfterSelectingPerson: (ABRecordRef) person
{
	[self.viewController dismissModalViewControllerAnimated:YES];
	
	//Getting the info from the selected contact.
	NSString *theSocialID = (NSString *)ABRecordCopyValue(person, kABPersonNoteProperty);
	
	//TODO: Compare with all social IDs in database for better comparions.
	if(theSocialID.length == 12)
	{
		NSString *theFirstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
		NSString *theLastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
		
		ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
		NSString *theDefaultEmail = (NSString *) ABMultiValueCopyValueAtIndex(emails, 0);
		ABMutableMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
		NSString *theDefaultPhone = (NSString *) ABMultiValueCopyValueAtIndex(phones, 0);
		
		WOPerson *selectedPerson = [[WOPerson alloc] initWithID:theSocialID
														   firstName:theFirstName
															lastName:theLastName
															   email:theDefaultEmail
															   phone:theDefaultPhone];
															   
		[delegate addContactCell:self didSelectPerson:selectedPerson];
		
		[selectedPerson release];
		[theFirstName release];
		[theLastName release];
		[theDefaultEmail release];
		[theDefaultPhone release];
		if (emails != NULL)
			CFRelease(emails);
		if (phones != NULL)
			CFRelease(phones);	
	}
	[theSocialID release];
	
	return NO;
}

- (BOOL) peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
	   shouldContinueAfterSelectingPerson:(ABRecordRef)person 
								 property:(ABPropertyID)property 
							   identifier:(ABMultiValueIdentifier)identifier
{
	[self.viewController dismissModalViewControllerAnimated:YES];
	return NO;
}

#pragma mark -

@end
